package cn.jizi.bshisy.controller;

import cn.jizi.bshisy.entity.InDoctor;
import cn.jizi.bshisy.service.InDoctorService;
import cn.jizi.bshisy.tool.PageSupport;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/inDoctor")
public class InDoctorController {
    private Logger logger = Logger.getLogger(InDoctorController.class);
    @Resource
    private InDoctorService inDoctorService;

    /*添加住院医师信息*/
    @ApiOperation(value = "添加住院医师信息", notes = "添加住院医师信息")
    @PostMapping(value = "/getAddInDoctor")
    public String getAddInDoctor(InDoctor inDoctor) {
        int count = inDoctorService.getAddInDoctor(inDoctor);
        if (count > 0) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }

    // 查询收费数据，支持模糊查询，分页功能
    @ApiOperation(value = "默认查全部然后根据姓名查询住院医信息", notes = "默认查全部然后根据姓名查询住院医信息")
    @GetMapping(value = "/getInNameInDoctorAllPage")
    public List<InDoctor> getInNameInDoctorAllPage(Model model, String inname, String pageIndex) {
        logger.info("==========================================》进入");
        logger.info("inname==================>" + inname);
        logger.info("pageIndex==================>" + pageIndex);
        List<InDoctor> inDoctorList = null;
       // 设置页面容量
        int pageSize = 3;
        // 当前页码
        int currentPageNo = 1;
        // 设置页码数
        if (pageIndex != null) {
        currentPageNo = Integer.valueOf(pageIndex);
        }
        // 总数量（表）
        int totalCount = 0;
        try {
            totalCount = inDoctorService.getInNameInDoctorListCount(inname);
        } catch (Exception e) {
            e.printStackTrace();
        }
     // 总页数
        PageSupport pages = new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        // 控制首页和尾页
        if (currentPageNo < 1) {
            currentPageNo = 1;
        } else if (currentPageNo > totalPageCount) {
            currentPageNo = totalPageCount;
        }
        try {

            inDoctorList = inDoctorService.getInNameInDoctorListPage(inname, currentPageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("inDoctorList========================" + inDoctorList);
        model.addAttribute("pages", pages);
        model.addAttribute("inDoctorList", inDoctorList);
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", currentPageNo);
        model.addAttribute("inname", inname);
        return inDoctorList;
    }

}
